<?use pft\Prod\Product;
include 'common/autoload.php';
include 'module/link_product/function.php';
include_once("class/ProductInfo.php");//
if(!$_SESSION['memberID']){exit("<script>window.location='dlogin.html'</script>");}
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$pid=$_REQUEST["pid"]; // 产品id
$lvl=$_REQUEST["lvl"]; // 转分销级别
$aids=$_REQUEST["aids"];// 供应商进化线
$sid=$_REQUEST['sid'];// 上级供应商
$sourceid=$_REQUEST["sourceid"]; // 原始供应商

// 获取会员分销价
$account = ($_SESSION['saccount'])? $_SESSION['saccount']:$_SESSION['account'];
$xml = $s->Dynamic_Price_And_Storage($account,$pid,date("Y-m-d"),1,0,0,$_REQUEST['sid']);
if($xml==105) $xml = $s->Dynamic_Price_And_Storage($account,$pid,date("Y-m-d"),2,0,0,$_REQUEST['sid']);
$object = simplexml_load_string($xml);
$fp=(int)$object->Rec->UUsprice; 
$fp = $fp/100;


// 价格配置价格
$setPri = abs($_REQUEST['one_price']);
//print_r($_REQUEST);exit;
$dPriset = ($setPri - $fp) * 100;// 分为单位

//echo $dPriset;
//exit;
// 找出账号分销商，排除产品供应商
$dist = array();
$sel="select m.id,dname,account from pft_member m left join pft_product_sale_list s on s.fid=m.id where m.status in (0,3) and s.status=0 and aid=".$_SESSION['sid']." and m.id<>".$sid." and m.id<>".$sourceid;
$GLOBALS['le']->query($sel);
while($row=$GLOBALS['le']->fetch_assoc()){
    $dist[$row['id']] = $row['id'];
}
//print_r($dist);echo count($dist);
//exit;
// 判断是否转分销
if($sid){
    // 转分销 操作表 pft_p_apply_evolute
    $exis = $exis_u = array();
    $sql="select id,fid,sid,sourceid,pid,lvl,rectime,aids from pft_p_apply_evolute where pid=$pid and sid={$_SESSION['sid']}";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        if(isset($dist[$row['fid']])){// 会员已经有存在记录
            $exis[] = $row['fid'];
            $id_update[] = $row['id'];
        }
    }

    $exis_u = array_diff($dist, $exis);// 需要新增记录的会员
    $ins    = 'insert into `pft_p_apply_evolute` (fid,sid,sourceid,pid,lvl,rectime,aids) values ';
    $lvl_n  = $lvl + 1 ;
    $aids_n = $aids.','.$_SESSION['sid'];
    if (count($exis_u)) {
        foreach($exis_u as $distID){
            $ins.="($distID,{$_SESSION['sid']},$sourceid,$pid,$lvl_n,now(),'$aids_n'),";
        }
        // echo rtrim($ins,',');
        $ins = rtrim($ins, ',');
//    echo '$ins=' . $ins . '<br/>';
        $GLOBALS['le']->query($ins);
    }
    if (count($id_update)) {
        foreach ($id_update as $id) {
            $sql_update = "UPDATE pft_p_apply_evolute SET aids='$aids_n' WHERE id=$id LIMIT 1";
            $GLOBALS['le']->query($sql_update);
//            echo $sql_update;
        }


//        $GLOBALS['le']->query($sql_update);
    }
}else{// 直销 操作表 pft_product_sale_list
    $sql = "SELECT fid,pids FROM pft_product_sale_list WHERE aid={$_SESSION['sid']}";
    $GLOBALS['le']->query($sql);
    $insert_arr = $update_arr = array();
    while($row=$GLOBALS['le']->fetch_assoc()) {
        if(isset($dist[$row['fid']])){// 会员已经有存在记录
            //判断是否已有这个产品
            if ($row['pids']=='A') continue;
            else {
                $pid_arr = explode(',', $row['pids']);
                if (in_array($pid, $pid_arr)) continue;
                else $update_arr[$row['fid']] = $row['pids'] . ',' . $pid;//pids中不存在这个产品，加入
            }
//            $exis[] = $row['fid'];
        }
        else {
            //不存在记录，
            $insert_arr[$row['fid']] = $pid;
        }
    }
    //更新已有数家
    if (count($update_arr)) {
        foreach ($update_arr as $fid=>$pids ) {
            $up = "UPDATE pft_product_sale_list SET pids='$pids' WHERE fid=$fid AND aid={$_SESSION['sid']} LIMIT 1";
//            echo '$up=' . $up . '<br/>';
            $GLOBALS['le']->query($up);
        }
    }
    //插入新数据
    if (count($insert_arr)) {
        $insert_sql = "INSERT INTO pft_product_sale_list(`fid`,`pids`,`status`) VALUES ";
        foreach ($insert_arr as $fid=>$pids) {
            $insert_sql .= "($fid, '{$pids}', 0),";
        }
        $insert_sql = rtrim($insert_sql, ',');
//        echo '$insert_sql=' . $insert_sql. '<br/>';
        $GLOBALS['le']->query($insert_sql);
    }
}
// 添加差价
$exis = $exis_u = $exis_uid = array();
$sql="select pid,id from `uu_priceset` where tid=$pid AND pid<>{$_SESSION ['sid']}";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()){
    $exis[$row['pid']]     = $row['pid'];// 获取已配置价格的分销商
    $exis_uid[]            = $row['id'];
}

// 未配置价格的分销商
$exis_u = array_diff($dist, $exis);
//var_dump(array_values($dist));
//var_dump($exis);
//var_dump($exis_u);
//print_r($exis_uid);
//exit;
if(count($exis_u)){
    $ins="insert into uu_priceset (pid,tid,dprice,aid) values ";
    foreach($exis_u as $distID){
        $ins.="($distID,$pid,'$dPriset',{$_SESSION['sid']}),";
    }
    $ins = rtrim($ins,',');
//    echo $ins;
    $GLOBALS['le']->query($ins);
}
// 更新已配置
if(count($exis_uid)){
    foreach($exis_uid as $uid){
        $upd="update `uu_priceset` set dprice='$dPriset' where id=$uid limit 1";
//        echo '<br/>'.$upd;
        $GLOBALS['le']->query($upd);
    }
}
echo "<script>alert('配置成功')</script>";
?>